#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Union

from sqlalchemy import String, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from backend.app.core.models.base import Base, id_key

from backend.app.module.dbms.models.dbms_instance_dept import dbms_instance_dept


class Instance(Base):
    """系统api"""

    __tablename__ = 'dbms_instance_user'
    id: Mapped[id_key] = mapped_column(init=False)
    instance_id: Mapped[str] = mapped_column(String(50), unique=True, comment='名称')
    instance_name: Mapped[str] = mapped_column(String(50), unique=True, comment='tag名称')
    instance_type: Mapped[int] = mapped_column(default=1, comment='实例类型（0主实例1从实例2备份实例3goincption4mgr实例）')
    source_type: Mapped[int] = mapped_column(default=1, comment='实例来源（0最高权限账号1复制账号2备份账号3监控账号4应用账号5同步账号6）')
    db_type: Mapped[int] = mapped_column(default=1, comment='数据库类型（0最高权限账号1复制账号2备份账号3监控账号4应用账号5同步账号6）')
    host: Mapped[str] = mapped_column(String(50), unique=True, comment='实例IP')
    vip: Mapped[str] = mapped_column(String(50), unique=True, comment='实例VIP')
    port: Mapped[int] = mapped_column(default=1, comment='角色状态（0最高权限账号1复制账号2备份账号3监控账号4应用账号5同步账号6）')
    username: Mapped[str] = mapped_column(String(50), unique=True, comment='tag名称')
    password: Mapped[str] = mapped_column(String(255), unique=True, comment='实例密码')
    admin_username: Mapped[str] = mapped_column(String(50), unique=True, comment='tag代码')
    admin_password: Mapped[str] = mapped_column(String(50), unique=True, comment='tag名称')
    admin_password: Mapped[str] = mapped_column(String(50), unique=True, comment='tag名称')
    status: Mapped[int] = mapped_column(default=1, comment='角色状态（0停用 1正常）')
    remark: Mapped[str | None] = mapped_column(String(255), comment='备注')
    tunnel_id: Mapped[int | None] = mapped_column(
        ForeignKey('tag.id', ondelete='SET NULL'), default=None, comment='标签关联ID'
    )
    instance: Mapped[Union['instance', None]] = relationship(init=False, back_populates='tags')


class InstanceLeater(CoreModel):
    """实例标签配置"""
    __tablename__ = 'dbms_instance_tag'

    instance_id = Column(Integer, primary_key=True, nullable=False, comment='实例ID')
    user_id = Column(Integer, primary_key=True, nullable=False, comment='用户ID')





